//
//  MainTarBar.swift
//  weibo
//
//  Created by 上官风雨 on 15/11/8.
//  Copyright © 2015年 上官风雨. All rights reserved.
//

import UIKit

class MainTarBar: UITabBar {
    


    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
       // fatalError("init(coder:) has not been implemented")
        
        super.init(coder: aDecoder)
        setupUI()
    }
    
    
    func setupUI (){
        
        addSubview(composeBtn)
    }
    //手动修改所有子控件的位置
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        let w = self.bounds.width / 5
        let h = self.bounds.height
        let rect = CGRectMake(0, 0, w, h)
        //定义递增变量
        var index :CGFloat = 0
        
        for subview in subviews {
            
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
                subview.frame = CGRectOffset(rect, w * index, 0)

                index += index == 1 ? 2 : 1
                
            }
            
        }
        composeBtn.frame = CGRectOffset(rect, w * 2, 0)
        bringSubviewToFront(composeBtn)
    }
    
    
    //懒加载撰写按钮
   lazy var composeBtn : UIButton = {
    
    let l = UIButton()
    
    l.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
    l.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
    
    l.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
    l.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
    
    l.sizeToFit()
    
    return l
    }()
    
    

}
